<?php
//$atomic = new Swoole\Atomic();
//(new Swoole\Process(function (Swoole\Process $process) use ($atomic) {
//    while ($atomic->get() < 5) {
//        $atomic->add(1);
//        echo "atomic 现在的值为：{$atomic->get()}   当前pid为：{$process->pid}\n" ;
//        sleep(1);
//    }
//    echo "pid {$process->pid} 结束\n";
//}))->start();
//
//(new Swoole\Process(function(Swoole\Process $process) use ($atomic) {
//    while ($atomic->get() < 10) {
//        $atomic->add(1);
//        echo "atomic 现在的值为：{$atomic->get()}   当前pid为：{$process->pid}\n" ;
//        sleep(1);
//    }
//    echo "pid {$process->pid} 结束\n";
//}))->start();
//
//Swoole\Process::wait();
//Swoole\Process::wait();


//$atomic = new Swoole\Atomic();
//
////$atomic->cmpset(0, 1);
//(new \Swoole\Process(function (\Swoole\Process $worker) use ($atomic) {
//    $atomic->wait(3);
//    echo "Shutdown wait Process: {$worker->pid}", PHP_EOL;
//
//}))->start();
//
//(new \Swoole\Process(function (\Swoole\Process $worker) use ($atomic) {
//    sleep(2);
//    $atomic->wakeup();
////    $atomic->cmpset(0, 1);
//    echo "Shutdown other Process: {$worker->pid}", PHP_EOL;
//}))->start();
//
//\Swoole\Process::wait();
//\Swoole\Process::wait();


//$lock = new Swoole\Lock();
//
//(new Swoole\Process(function (Swoole\Process $process) use ($lock) {
//    $lock->lock();
//    sleep(2);
//    echo "1先开始\n";
//    $lock->unlock();
//}))->start();
//
//
//(new Swoole\Process(function (Swoole\Process $process) use ($lock) {
//    $lock->lock();
//    echo "2先开始\n";
//    $lock->unlock();
//}))->start();
//
//Swoole\Process::wait();
//Swoole\Process::wait();




//$table = new Swoole\Table(1024);
//$table->column('worker_id', Swoole\Table::TYPE_INT);
//$table->column('count', Swoole\Table::TYPE_INT);
//$table->column('data', Swoole\Table::TYPE_STRING,64);
//$table->create();
//$ppid = getmypid();
//$table->set($ppid, ['worker_id'=>getmypid(), 'count'=>0, 'data'=>"这里是 " . $ppid]);
//
//(new Swoole\Process(function (Swoole\Process $worker) use ($ppid,$table) {
//    $table->set($worker->pid, ['worker_id' => $worker->pid, 'count' => 0, 'data' => "这里是{$worker->pid}"]);
//    sleep(1);
//    $table->incr($worker->pid, 'count');
//    print_r($table->get($worker->pid));
//}))->start();
//
//(new Swoole\Process(function(Swoole\Process $worker) use ($ppid,$table) {
//    $table->set($worker->pid, ['worker_id' => $worker->pid, 'count' => 3, 'data' => "这里是{$worker->pid}"]);
//    sleep(1);
//    $table->decr($worker->pid, 'count');
//    print_r($table->get($worker->pid));
//    sleep(1);
//    echo "{$worker->pid} 内部循环：", PHP_EOL;
//    foreach($table as $t){
//        print_r($t);
//    }
//    if($table->exist($ppid)){
//        $table->del($ppid);
//    }
//}))->start();
//
//\Swoole\Process::wait();
//\Swoole\Process::wait();
//
//echo "Table数量 : {$table->count()} \n";
//echo "主进程循环: \n" ;
//foreach($table as $t){
//    print_r($t);
//}
//echo "Table 状态：", PHP_EOL;
//print_r($table->stats());